package nccloud.web.hrpbm;

import nc.vo.bd.util.ArrayUtil;
import nc.vo.hrpbm.budgetswap.AggBudgetswapVO;
import nc.vo.hrpbm.budgetswap.BudgetswapBVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.ISuperVO;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author Administrator
 * @Date 2025年3月19日
 */
public class HrpbmHandlerUtil {

    /**
     *
     * 检查预算调试是否存在转出对转入一对多的情况
     * @param vos  预算调剂 aggvos
     * @throws BusinessException
     */
    public static void checkBudgetsWapIsRepeat(AggBudgetswapVO[] vos) throws BusinessException {
        if(vos == null || ArrayUtil.isEmpty(vos)) throw new BusinessException("请仔细检查,单据信息不允许为空!");
        HrpbmHandlerUtil util = new HrpbmHandlerUtil();
        StringBuilder error = new StringBuilder();
        for (AggBudgetswapVO vo : vos) {
            ISuperVO[] bvos = vo.getChildren(BudgetswapBVO.class);
            if(bvos == null || ArrayUtil.isEmpty(bvos)) continue;
            boolean isExist = util.checkForEqualAndDifferentValues(bvos);
            if(isExist) {
                error.append("同一预算调剂单中 转出科室与转入科室只允许存在一对一");
            }
        }
        if(error.length() > 0) throw new BusinessException(error.toString());
    }

    /**
     * 检查是否存在值相同，与不同的数据
     * @param bvos
     * @return
     */
    public boolean checkForEqualAndDifferentValues(ISuperVO[] bvos) throws BusinessException {
        HashSet<Object> outs = new HashSet<>();
        HashSet<Object> ins = new HashSet<>();
        Boolean flag = false;
        for (ISuperVO bvo : bvos) {
            if(bvo.getStatus() != 3 ){ // 3 为删除
                Object pk_budgetdept_out = bvo.getAttributeValue("pk_budgetdept_out"); // 转出
                Object pk_budgetdept_in = bvo.getAttributeValue("pk_budgetdept_in"); // 转入
                if(pk_budgetdept_out != null){
                    outs.add(pk_budgetdept_out);
                }
                if(pk_budgetdept_in != null){
                    ins.add(pk_budgetdept_in);
                }
                if(pk_budgetdept_out.toString().equals(pk_budgetdept_in.toString())) {
                    flag = true; // 不允许同科室转入转出
                }

            }
        }
        if (outs.size() > 1 )  return true;
        if (ins.size() > 1 )  return true;
        if(flag) throw new BusinessException("转出科室与转入科室不允许一样");
        return false; // 没有找到值相同且值不同的情况
    }
}
